import {HttpClient} from "@angular/common/http";
import {Injectable} from "@angular/core";
import {Observable} from "rxjs/Observable";
import {DOMAIN_SERVER_URL} from "../constants";
import {User} from "../entity/User";
import {BaseService} from "./baseService";
import {RestUser} from "../entity/RestUser";
import {RestResult} from "../entity/rest-result";
import {RestWechat} from "../entity/RestWechat";

@Injectable()
export class UserService extends BaseService<User> {

    constructor(http: HttpClient) {
        super(http);
    }

    getServiceUrl(): string {
        return DOMAIN_SERVER_URL ;
    }


    login(username: string, password: string): Observable<any> {
        return this.http
            .post(this.getServiceUrl() + "/login", {
                userid: username,
                passwd: password
            })
            .catch(this.handleError);
    }

    get(id: number): Observable<RestUser> {
      return this.http.get<RestResult<RestUser>>(this.getServiceUrl() + `/windbell/user/${id}`).map((res) => { return res.value })
    }

    hasWechatUser(openId: string): Observable<any> {
      return this.http.get<RestResult<any>>(this.getServiceUrl() + `/windbell/wechat/${openId}` ).map((res)=>{
        return res.value
      })
    }

    wechatLogin(openid: string): Observable<any> {
      return this.http
        .post(this.getServiceUrl() + "/wechatlogin", {
          openid: openid
        })
        .catch(this.handleError);
    }

    registerWeChatUser(restWechat: RestWechat): Observable<any> {
      return this.http.post<RestResult<any>>(this.getServiceUrl() + "/windbell/wechat/binding" , restWechat).map((res)=>{
        return res.value
      })
    }
    unregisterWeChatUser(restWechat: RestWechat): Observable<any> {
      return this.http.post<RestResult<any>>(this.getServiceUrl() + "/windbell/wechat/unbinding" , restWechat).map((res)=>{
        return res.value
      })
    }

    registerWeChatUserByPhone(restWechat: RestWechat): Observable<any> {
      return this.http.post<RestResult<any>>(this.getServiceUrl() + "/windbell/wechatuser/binding" , restWechat).map((res)=>{
        return res.value
      })
    }
    unregisterWeChatUserByPhone(restWechat: RestWechat): Observable<any> {
      return this.http.post<RestResult<any>>(this.getServiceUrl() + "/windbell/wechatuser/unbinding" , restWechat).map((res)=>{
        return res.value
      })
    }

}
